#!/usr/bin/env python
# -*- coding: utf-8 -*-
from interface import run, yml2Python
import click
import sys,os
import time
from common.tools import setEnv
from common import  BASEDIR, L

@click.group()
@click.option('--log-level', '-L',
              help='设置运行时日志级别',
              default='info',
              required=False,
              type=click.Choice(['info', 'debug', 'warn', 'error', 'critical'], case_sensitive=False))
@click.version_option(version='2.0.0', message='autoTest %(version)s')
def cli(log_level):
    if BASEDIR not in sys.path:
        sys.path.append(BASEDIR)
    L.setLevel(log_level.upper())
    click.echo(
        click.style(
            'start autoTest console log as "{}"'.format(log_level),
            fg='green'
        )
    )

@cli.command()
def check():
    """检查运行环境"""
    from common.environment import Environment
    Environment()


@cli.command()
@click.option('--report',
              help='junit 和 html报告的(路径)名字',
              required=False,
              type=click.Path())
@click.option('--dot-env',
              help='环境变量选项',
              required=False,
              type=click.Path())
@click.option('--tag', '-t',
              help='过滤测试数据',
              required=False,
              type=str)
@click.option('--options', '-O',
              help='set chrome options',
              default='off',
              required=False,
              type=click.Choice(['on', 'off'], case_sensitive=False))
@click.option('--pt',
              help="set pytest's commands in quotes",
              required=False,
              type=str)
@click.argument('case', required=False, nargs=-1)
def rt(case, report, dot_env, tag, options, pt):
    """
    [casePath|case.py ...] run testcases [default: test/*]
    """
    setEnv(mode=options)
    msg = '{} is going to run'.format(' '.join(case)) if case else 'will run by default'
    msg = '{} {}'.format(pt if pt else '', msg)
    click.echo(click.style(
        msg,
        fg='cyan'
    ))

    if not report:
        report = os.path.join('report', time.strftime(
            '%Y%m%d_%H%M%S', time.localtime(time.time())
        ))
    click.echo(click.style(
        'the report will store in {}'.format(report),
        fg='cyan'
    ))

    if not tag:
        setEnv(tag = '')
    else:
        setEnv(tag=tag.strip())
        click.echo(
            click.style(
                'tag include "{}" will run'.format(tag), fg='cyan'
            )
        )

    try:
        result = run(case, report, dot_env, pt)
    except Exception as e:
        raise e

    return result

@cli.command()
@click.option('--pages', '-P',
              help='设置目标文件名',
              default='pages.py',
              required=False,
              type=click.Path())
@click.argument('yml_path', type=click.Path(exists=True), required=True, nargs=1)
def y2p(yml_path, pages):
    """
    parse .yml to .py by template
    """
    yml_path = os.path.join(BASEDIR, yml_path)
    yml2Python(yml_path, pages)


if __name__ == '__main__':
    sys.exit(cli())
